﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
using gather;
using System.Resources;
using GCELibrary.Current;
using ZF.BLL;
using System.Threading;
using System.Diagnostics;
using ZF.BLL.Gather;
namespace ConsoleApplication4
{
    class cz:gbase
    {

   
        public override void Start()
        {
            GatherListing = true;
            var p = this;
            var dic = p.getCityTemplate();

            ThreadPool.QueueUserWorkItem(new WaitCallback(p.StartGatherBySiteThread1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(p.CheckTimeOutThread));


            foreach (var item in dic)
            {
                p.StartGatherBySiteThread(item);

            }
            GatherListing = false;
        }

        public void StartGatherBySiteThread(object o)
        {

            var kvp = (KeyValuePair<string, SimpleTree<int>>)o;
            var urlDomain = string.Format("http://{0}.ganji.com/", kvp.Key);

            HttpClient hc = new HttpClient();

            var pages = new string[] { "", "f32/", "f64/", "f96/", "f128/", "f160/", "f192/", "f224/" }.ToList();
            var html = "";
            pages.ForEach(p =>
            {
                var url = string.Format("{0}/fang1/{1}", urlDomain, p);
                try
                {
                    html += hc.GetSrc(url);
                }
                catch (Exception) { }
            });
            var spliteReg = @"class=""list_title"" href=""(.+?)""";
            Regex regex = new System.Text.RegularExpressions.Regex(spliteReg);
            var ms = regex.Matches(html);
            List<string> urlList = new List<string>();
            var cstrs = new string[] { 
                    DateTime.Now.ToString("yyMMdd"),
                    DateTime.Now.AddDays(-1).ToString("yyMMdd"),
                   "tuiguang"
            
            }.ToList() ;
 
            foreach (Match m in ms)
            {

                var url=urlDomain + m.Groups[1].Value;


                if (cstrs.Exists(p => url.Contains(p)) && !cacheUrl.Contains(url))
                {
                    string pageHtml = null; try
                    {
                        pageHtml = hc.GetSrc(url);
                        JXList.Add(new JXParam() { Html = pageHtml, Url = url, Domain = urlDomain, City = kvp.Value });
                        Debug.Print(url);
                    }
                    catch (Exception ex)
                    {
                    }

                    
                }
                //Console.WriteLine(m.Groups[1].Value);
            }
            //var htmls = getInfoHTML(urlList.ToArray());
            //var modelList = htmls.Select(p => getModel(p, "", kvp.Value)).ToList();
        }
   
          public List<string> cacheUrl = File.ReadAllLines("cache/cachecz.txt").ToList();

          public void StartGatherBySiteThread1(object o)
        {
            gather.ServiceReference1.WebServiceSoapClient sc = new gather.ServiceReference1.WebServiceSoapClient();
           
            Action<object> action = p =>
                {
                    var jx = (JXParam)p;
                    jx.Status = 1;
                    getModel((JXParam)p);
                    jx.Status = 3;
                    lock (lockJS) cRunCount--;
                    if (jx.HouseModel != null)
                    {

                        
                       
                        if (sc.GatherHouse(jx.HouseModel.JsonConvert(), jx.Url, jx.HouseModel.telimg.Contains("png"),1))
                        {
                            lock (cacheUrl)
                            {
                                try
                                {
                                    File.AppendAllText("cache/cachecz.txt", jx.Url + "\r\n");
                                }
                                catch (Exception ex) { }
                            }
                            cacheUrl.Add(jx.Url);
                            Console.WriteLine("更新成功===========================" + jx.Url);
                        }
                        else
                        {
                            Console.WriteLine("更新失败===========================" + jx.Url);
                        }
                        
                    }

                };
            while (true)
            {

                while (cRunCount < 5 && JXList.Count>0)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(action));
                    var jx = JXList[0];
                    JXList.RemoveAt(0);
                    t.Start(jx);
                    JXListFinish.Add(jx);
                    lock (lockJS) cRunCount++;
                    //Console.Read();
                }
                Thread.Sleep(100);
            }

        }

          public HouseGatherModel getModel(JXParam jx)
        {
            bool iszj = jx.Html.Contains("tel_number") ;
            var html = jx.Html;
            var key = jx.Url;
            var city = jx.City;
            jx.BT = DateTime.Now;
            AddCheckTimeOut(jx);
            var reg = iszj ? regZJ : regInfo;
            var m = reg.Match(html);
          
           GroupCollection gc = null;
          
           if (!m.Success)
           {

               Console.WriteLine("++++++++++++cz采集错误++++++++++++");
               return null;
               throw new Exception("getModel");
           }
           RemoveCheckTimeOut(jx);
           var g = m.Groups;
           #region
           HouseGatherModel hm = new HouseGatherModel()
           {
               title = g[1].Value,
               price = g[2].Value,
           
               面积 = g[3].Value,
               //小区 = g[4].Value,
               area1 = g[5].Value,
               address = g[6].Value,
               tin = g[7].Value,
               装修 = g[8].Value,
               朝向 = g[9].Value,
               clo = g[10].Value,
               config = g[11].Value,
               contract = g[12].Value,
               telimg = g[13].Value,
               info = g[14].Value,
                HouseType="住宅",
               num = key,
           };
           //==标题，时间
           reg = new Regex(@"<h1(?: class=""f_s_13"")?>((?:.|\s)*?)<span class=""pub_time"">((?:.|\s)*?)</span>");
           m = reg.Match(hm.title);

           hm.title = m.Groups[1].Value;


           var treg = new Regex(@"\d{2}-\d{2}\s\d{2}:\d{2}");
           if (treg.IsMatch(m.Groups[2].Value))
           {
               hm.Time = string.Format("2010-{0}:00", m.Groups[2].Value).Convert<DateTime>();
           }
           else
               hm.Time = m.Groups[2].Value.Convert<DateTime>();

         
           //==价格，支付方式
           reg = new Regex(@"f_c_red f_s_24"">(\d+)</span>(?:(?:.|\s)*?(?:\(|（)((?:.|\s)*?)(?:\)|）))?");
           m = reg.Match(hm.price);
           hm.price = m.Groups[1].Value;
           hm.Payment = m.Groups[2].Value;
           //==面积
           reg = new Regex(@">(\d+)");
           m = reg.Match(hm.面积);
           hm.面积 = m.Groups[1].Value;
           //==小区
           //reg = iszj ? new Regex(@" target=""_blank"">(.*?)</a>") : new Regex(@"<dd>(.*?)</dd>");
           //m = reg.Match(hm.小区);
           //hm.小区 = m.Groups[1].Value;
           //==城市
           reg = new Regex(@""">((?:.|\s)*?)</a>(?:(?:.|\s)*?"">((?:.|\s)*?)</a>)?");
           m = reg.Match(hm.area1);
           hm.area1 = m.Groups[1].Value;
           hm.area2 = m.Groups[2].Value;
           var carea1 = city.Childs.FirstOrDefault(p => p.Name == hm.area1);
           if (carea1 == null)
               carea1 = city.Childs.FirstOrDefault(p => p.Name.Contains(hm.area1) || hm.area1.Contains(p.Name));

           if (city.Name == "广州市")
           {
               if (carea1 != null) { hm.area1 = "------" + carea1.Name; hm.area1Id = carea1.Id; }

               if (carea1 != null && city.Name == "广州市" && hm.area2 != "")
               {
                   var carea2 = carea1.Childs.FirstOrDefault(p => p.Name == hm.area2);
                   if (carea2 == null)
                       carea2 = carea1.Childs.FirstOrDefault(p => p.Name.Contains(hm.area2) || hm.area2.Contains(p.Name));
                   if (carea2 != null) { hm.area2 = "------" + carea2.Name; hm.area2Id = carea2.Id; }
               }
           }
           else
           {
               hm.area1 = "------" + city.Name;
               hm.area1Id = city.Id;
               if (carea1 != null)
               {
                   hm.area2 = "------" + carea1.Name;
                   hm.area2Id = carea1.Id;
               }
           }

           
           





             //==地址
           reg = new Regex(@">(.*?)<");
           m = reg.Match(hm.address);
           hm.address = m.Groups[1].Value;

           //==房型
           reg = new Regex(@"<dd>\s?(.*?)\s-\s(\d).(\d).(\d).");
           m = reg.Match(hm.tin);
           hm.DetpType = m.Groups[1].Value;
           hm.fan = m.Groups[2].Value;
           hm.tin = m.Groups[3].Value;
           hm.wei = m.Groups[4].Value;

           //==装修
           reg = new Regex(@">(.*?)<");
           m = reg.Match(hm.装修);
           hm.装修 = m.Groups[1].Value;

           //==朝向
           reg = new Regex(@">(.*?)<");
           m = reg.Match(hm.朝向);
           hm.朝向 = m.Groups[1].Value;

            //==楼层
           reg = new Regex(@"第(.*?)层/总(.*?)层");
           m = reg.Match(hm.clo);
           hm.clo = m.Groups[1].Value;
           hm.tlo = m.Groups[2].Value;

           //==配置
           reg = new Regex(@">(.*?)<");
           m = reg.Match(hm.config);
           hm.config = m.Groups[1].Value;

 

             //==说明
           reg = new Regex(@"(?:id_description"">|""text"">)((?:.|\s)*)");
           m = reg.Match(hm.info);
           hm.info = m.Groups[1].Value;
           #endregion
           if (hm.area1 == "" || hm.price == "" || hm.title == "")
           {
           }
           if (!iszj)
           {
               var hc = new HttpClient();
              hm.imgBuff= hc.DownloadData(jx.Domain + "/tel/" + hm.telimg);
               
           }
         

           Print(hm);
           jx.HouseModel = hm;
           return hm;
           
        }

 
          public Regex regInfo = new Regex(gather.Resource1.Info, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
          public Regex regZJ = new Regex(gather.Resource1.InfoZJ, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

           
 
    }


 

}
